package cn.hznc.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.LogEntity;
import cn.hznc.domain.entity.UserEntity;
import cn.hznc.mapper.LogMapper;
import cn.hznc.repository.UserRepository;
import cn.hznc.utils.CommonUtil;
import cn.hznc.utils.JwtHelper;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Author：Administrator
 * @Date：2024/1/11 16:23
 */
@Component
@Aspect
public class LogAspect{

    @Resource
    private LogMapper logMapper;

    @Resource
    private UserRepository userRepository;

    @Pointcut("@annotation(cn.hznc.aspect.Log)")
    public void logPointCut(){


    }



    @Around("@annotation(cn.hznc.aspect.Log)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        //拿到目标方法
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method targetMethod = methodSignature.getMethod();
        //拿到注解上面的参数
        Log shopCache = targetMethod.getAnnotation(Log.class);
        return handleLog(pjp , shopCache);
    }

    protected Object handleLog(final ProceedingJoinPoint joinPoint , Log controllerLog) throws Throwable{
        if(ObjectUtil.isNotNull(controllerLog)){
            String title = controllerLog.title();
            BusinessType businessType = controllerLog.businessType();

            LogEntity logEntity = new LogEntity();
            logEntity.setTitle(title);
            logEntity.setOperate(businessType.name());
            logEntity.setOperateType(businessType.ordinal());
            buildLogEntity(logEntity , joinPoint);
            logMapper.insert(logEntity);
            Object proceed = joinPoint.proceed();
            return proceed;
        }
        return null;
    }

    private void buildLogEntity(LogEntity logEntity, JoinPoint joinPoint) {
        //处理业务逻辑
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取请求url
        String url = request.getRequestURI();
        //获取请求方式
        String method = request.getMethod();
        //获取请求类名
        String className = joinPoint.getSignature().getDeclaringTypeName();
        //获取请求方法名
        String methodName = joinPoint.getSignature().getName();
        logEntity.setIp(IpUtil.getIpAddress(request));
        logEntity.setRequestRul(url);
        logEntity.setCreateTime(new Date());
        logEntity.setMethod(methodName);
        logEntity.setRequestMethod(method);
//        Integer userId = CommonUtil.getUserId();
        String token = request.getHeader("Authorization");
        if(StringUtils.isNotBlank(token)){
            Long userId = JwtHelper.getUserId(token);
            if(ObjectUtil.isNotNull(userId)){
                UserEntity userEntity = userRepository.selectbyUserid(userId.intValue());
                if(ObjectUtil.isNotNull(userEntity)){
                    logEntity.setOperatorName(userEntity.getEmployeeName());
                    logEntity.setEmployeeDept(userEntity.getCompany());
                }
            }
        }
    }

}
